$(function(){
	var recommendData=null;
	var newestCourseData=null;
	var allCourseData=null;
	var siftCourseData=null;
	var bannerData=null;
	var advertisementData=null;
	var newestInformationData=null;
	var hotInformationData=null;
	var evaluateData=null;
	var trendsData=null;
	var classifyIds = ['1d7939df-7758-11e7-974c-487b6bd31f43','3d6be52f-6dff-11e7-974c-487b6bd31f43','745555c0-578c-11e7-974c-487b6bd31f43'];
	//跳转课程标题
	//$(".txtOf .omit").attr("style","height:22px");
	//为你推荐
	/*$.ajax({
		type:"get",
		url:"../portal/courselibrary/findCourseByLabelCode.json?labelCode=uuid-label-recourse&queryCount=4",
		async:true,
		success:function(data){
			if(data.success){
			//取出所有时间并重新赋给对象
			for(var i=0;i<data.data.listCourse.length;i++){
						var time = new Date(data.data.listCourse[i].modifyTime);			
						var m=time.getMonth()+1;
						var d=time.getDate();
						var showtime=m+"/"+d;
						data.data.listCourse[i].modifyTime=showtime;
		            }
            //生成模板函数，填充数据，写入html
			recommendData=data.data;
			var recommendTpl=_.template($("#recommendTpl").html());
			var recommendHtml=recommendTpl(recommendData);
			$("#recommend").html(recommendHtml);
			}
		}
	});*/
	//在线课件
	$.ajax({
		type:"get",
		url:"../portal/courselibrary/findResourceClassify.json",
		success:function(data){
			if(data.success){
				onlineResourceData=data.data;
				for(var i=0;i<onlineResourceData.classifyList.length;i++){
					if(onlineResourceData.classifyList[i].classifyId=='1d7939df-7758-11e7-974c-487b6bd31f43'){
						onlineResourceData.classifyList[i].showImg = "/images/resource_01.png";
					}else if(onlineResourceData.classifyList[i].classifyId=='3d6be52f-6dff-11e7-974c-487b6bd31f43'){
						onlineResourceData.classifyList[i].showImg = "/images/resource_02.png";
					}else if(onlineResourceData.classifyList[i].classifyId=='745555c0-578c-11e7-974c-487b6bd31f43'){
						onlineResourceData.classifyList[i].showImg = "/images/resource_03.png";
					}
				}
				var onlineResourceTpl=_.template($("#onlineResourceTpl").html());
				var onlineResourceHtml=onlineResourceTpl(onlineResourceData);
				$("#onlineResource").html(onlineResourceHtml);
			}
		}
	});
	//最新课程
	$.ajax({
		type:"get",
		url:"../portal/courselibrary/findCourseNew.json?queryCount=8",
		async:true,
		success:function(data){
			if(data.success){
			//取出所有时间并重新赋给对象
			for(var i=0;i<data.data.listCourse.length;i++){
						var time = new Date(data.data.listCourse[i].modifyTime);			
						var m=time.getMonth()+1;
						var d=time.getDate();
						var showtime=m+"/"+d;
						data.data.listCourse[i].modifyTime=showtime;
		            }
          //生成模板函数，填充数据，写入html
			newestCourseData=data.data;
			var newestCourseTpl=_.template($("#recommendTpl").html());
			var newestCourseHtml=newestCourseTpl(newestCourseData);
			$("#newestCourse").html(newestCourseHtml);
			}
		}
	});
	//全部课程
	$("#allCourseLi").on("mouseenter",function(){
		//判断是否有数据，如果没有进行ajax
		if(!allCourseData){
			$.ajax({
				type:"get",
				url:"../portal/courselibrary/findCourseHot.json?queryCount=8",
				async:true,
				success:function(data){
					if(data.success){
					//取出所有时间并重新赋给对象
					for(var i=0;i<data.data.listCourse.length;i++){
						var time = new Date(data.data.listCourse[i].modifyTime);			
						var m=time.getMonth()+1;
						var d=time.getDate();
						var showtime=m+"/"+d;
						data.data.listCourse[i].modifyTime=showtime;
		            }
		            //生成模板函数，填充数据，写入html
		           
					allCourseData=data.data;
					var newestCourseTpl=_.template($("#recommendTpl").html());
					var newestCourseHtml=newestCourseTpl(allCourseData);
					$("#allCourse").html(newestCourseHtml);
					}
				}
			});
		}
	});
	//精品课程
	/*$("#siftCourseLi").on("mouseenter",function(){
		//判断是否有数据，如果没有进行ajax
		if(!siftCourseData){
			$.ajax({
				type:"get",
				url:"../portal/courselibrary/findCourseByLabelCode.json?labelCode=uuid-label-Boutique&queryCount=8",
				async:true,
				success:function(data){
					if(data.success){
					//取出所有时间并重新赋给对象
					
					for(var i=0;i<data.data.listCourse.length;i++){
						var time = new Date(data.data.listCourse[i].modifyTime);			
						var m=time.getMonth()+1;
						var d=time.getDate();
						var showtime=m+"/"+d;
						data.data.listCourse[i].modifyTime=showtime;
		            }
		            //生成模板函数，填充数据，写入html
					siftCourseData=data.data;
					var siftCourseTpl=_.template($("#recommendTpl").html());
					var siftCourseHtml=siftCourseTpl(siftCourseData);
					$("#siftCourse").html(siftCourseHtml);
					}
				}
			});
		}
	});*/
	//banner图
	$.ajax({
		type:"get",
		url:"../portal/advert/findAdvertPosition.json?positionCode=index-banner",
		async:true,
		success:function(data){
			if(data.success){
            //生成模板函数，填充数据，写入html
			bannerData=data;
			var bannerTpl=_.template($("#bannerTpl").html());
			var bannerHtml=bannerTpl(bannerData);
			$("#banner").html(bannerHtml);
			if(data.data.length == 1){
				$("#bannerW").attr("style","background: url(../components/gtattachment/open/downloadAttachment.json?attachmentId="+data.data[0].adImage+")  center no-repeat;")
				return;
			}
				$.getScript("../common/klxlearning/koala.min.1.5.js",function(){
				$(".main_visual").hover(function(){
			      $("#btn_prev,#btn_next").fadeIn()
			      },function(){
			      $("#btn_prev,#btn_next").fadeOut()
			      })
			    $dragBln = false;
			  $(".main_image").touchSlider({
			      flexible : true,
			      speed : 200,
			      btn_prev : $("#btn_prev"),
			      btn_next : $("#btn_next"),
			      paging : $(".flicking_con a"),
			      counter : function (e) {
			        $(".flicking_con a").removeClass("on").eq(e.current-1).addClass("on");
			      }
			    });
			    $(".main_image").bind("mousedown", function() {
			      $dragBln = false;
			    })
			    $(".main_image").bind("dragstart", function() {
			      $dragBln = true;
			    })
			    $(".main_image a").click(function() {
			      if($dragBln) {
			        return false;
			      }
			    })
			    timer = setInterval(function() { $("#btn_next").click();}, 3000);
			    $(".main_visual").hover(function() {
			      clearInterval(timer);
			    }, function() {
			      timer = setInterval(function() { $("#btn_next").click();}, 3000);
			    })
			    $(".main_image").bind("touchstart", function() {
			      clearInterval(timer);
			    }).bind("touchend", function() {
			      timer = setInterval(function() { $("#btn_next").click();}, 3000);
			    })
			});	
		 };
		}
	});
	//广告
//	$.ajax({
//		type:"get",
//		url:"../common/klxlearning/index1.json",
//		async:true,
//		success:function(data){
//			if(data.success){
//        //生成模板函数，填充数据，写入html
//			advertisementData=data.data;
//			var advertisementTpl=_.template($("#advertisementTpl").html());
//			var advertisementHtml=advertisementTpl(advertisementData);
//			$("#advertisement").html(advertisementHtml);
//			}
//		}
//	});
	//最新资讯
	$.ajax({
		type:"get",
		url:"../portal/information/findLabelInfo.json?labelCode=HOTINFORMATION",
		async:true,
		success:function(data){
			if(data.success){
			//取出所有时间并重新赋给对象
			//只有当资讯条数达到3条时，才开始进行界面渲染
			if(data.data&&data.data.length>=3){
				for(var i=0;i<data.data.length;i++){
					var time = new Date(data.data[i].publishTime);			
					var m=time.getMonth()+1;
					var d=time.getDate();
					var showtime=m+"/"+d;
					data.data[i].publishTime=showtime;
		            }
	            //生成模板函数，填充数据，写入html
				var newestInformationTpl=_.template($("#informationTpl").html());
				var newestInformationHtml=newestInformationTpl(data);
				$("#HotInformation").append(newestInformationHtml);
			}
			}
		}
	});
	//热点新闻
	$.ajax({
		type:"get",
		url:"../portal/information/findLabelInfo.json?labelCode=HOTNEWS",
		async:true,
		success:function(data){
			if(data.success){
			//取出所有时间并重新赋给对象
			//只有当资讯条数达到3条时，才开始进行界面渲染
			if(data.data&&data.data.length>=3){
				for(var i=0;i<data.data.length;i++){
					var time = new Date(data.data[i].publishTime);			
					var m=time.getMonth()+1;
					var d=time.getDate();
					var showtime=m+"/"+d;
					data.data[i].publishTime=showtime;
		            }
	            //生成模板函数，填充数据，写入html
				var hotInformationTpl=_.template($("#informationTpl").html());
				var hotInformationHtml=hotInformationTpl(data);
				$("#Hotnews").append(hotInformationHtml);
			}
			}
		}
	});
	//学员评价
	$.ajax({
		type:"get",
		url:"../portal/evaluate/studentEvaluate.json?configCode=COURSE&queryCount=4",
		async:true,
		success:function(data){
			if(data.success){
			//取出所有时间并重新赋给对象
			for(var i=0;i<data.data.orgEvaluateList.length;i++){
				var time = new Date(data.data.orgEvaluateList[i].evaluateBean.editTime);			
				var m=time.getMonth()+1;
				var d=time.getDate();
				var showtime=m+"/"+d;
				data.data.orgEvaluateList[i].evaluateBean.editTime=showtime;
				
	        	if(data.data.orgEvaluateList[i].evaluateBean.accountBean.headPhoto){
	        		
					data.data.orgEvaluateList[i].evaluateBean.accountBean.headPhoto="../components/gtattachment/open/downloadAttachment.json?attachmentId="+data.data.orgEvaluateList[i].evaluateBean.accountBean.headPhoto;	
				}
				else{
					 data.data.orgEvaluateList[i].evaluateBean.accountBean.headPhoto="../images/klxlearning/man.png";
				}    
			}
            //生成模板函数，填充数据，写入html
			evaluateData=data.data;
			var evaluateTpl=_.template($("#evaluateTpl").html());
			var evaluateHtml=evaluateTpl(evaluateData);
			$("#evaluate").html(evaluateHtml);
			}
		}
	});
	//学员动态
	$.ajax({
		type:"get",
		url:"../portal/courselibrary/findLearninfoUserList.json?queryCount=4",
		async:true,
		success:function(data){
			if(data.success){
				for(var i=0;i<data.data.listCourse.length;i++){
				var time = new Date(data.data.listCourse[i].lastLearnDate);			
				var m=time.getMonth()+1;
				var d=time.getDate();
				var showtime=m+"/"+d;
				data.data.listCourse[i].lastLearnDate=showtime;
				if(data.data.listCourse[i].userBean.headPhoto){
					data.data.listCourse[i].userBean.headPhoto="../components/gtattachment/open/downloadAttachment.json?attachmentId="+data.data.listCourse[i].userBean.headPhoto	
				}
				else{
					 data.data.listCourse[i].userBean.headPhoto="../images/klxlearning/man.png";
				}
			}
            //生成模板函数，填充数据，写入html
			trendsData=data.data;
			
			var trendsTpl=_.template($("#trendsTpl").html());
			var trendsHtml=trendsTpl(trendsData);
			$("#trends").html(trendsHtml);
			}
		}
	});
});  
